package com.wl.api.controller.business;

import java.util.Objects;
import java.util.function.Predicate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.sun.org.omg.SendingContext.CodeBase;
import com.wl.api.common.utils.BeanUtils;
import com.wl.api.common.utils.ErrorCodeEnum;
import com.wl.api.common.utils.JsonObject;
import com.wl.api.common.utils.StringUtil;
import com.wl.api.controller.base.CommonController;
import com.wl.api.dto.base.CommonUserDTO;
import com.wl.api.dto.business.CustomerAddrFromDTO;
import com.wl.api.model.business.CustomerAddrFrom;
import com.wl.api.service.business.ICustomerAddrFromService;

/**
 *  客户 发件人地址管理
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/external/customer/addrFrom")
public class CustomerAddrFromController extends CommonController {
 
	@Autowired
	ICustomerAddrFromService addrFromService;
	
	
	@RequestMapping(value="/manage", method = RequestMethod.POST)
	JsonObject create(@RequestBody CustomerAddrFromDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		String title = "", code =""; 
		boolean pass = true;
		if (validate().or(s -> s.getFromAddrId() != null).test(param)) {
			title = ErrorCodeEnum.param_error.getError_code();
			code = ErrorCodeEnum.param_error.getMessage();
			pass = false;
		}
		if (pass) {
			initData(param);
			CustomerAddrFrom entity = BeanUtils.copyProperties(param, CustomerAddrFrom.class);
			addrFromService.insert(entity);
			json.setData(entity.getFromAddrId());
		}
		setJsonMessage(json, title, code, pass);
		return json;
	}
	
	@RequestMapping(value="/manage", method= RequestMethod.PUT)
	JsonObject modify(@RequestBody CustomerAddrFromDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		CommonUserDTO user = getCommonUser();
		String title = "", code =""; 
		boolean pass = true;
		if (validate().or(s -> s.getFromAddrId() == null).test(param)) {
			title = ErrorCodeEnum.param_error.getError_code();
			code = ErrorCodeEnum.param_error.getMessage();
			pass = false;
		}
		if (pass) {
			initData(param);
			addrFromService.update(BeanUtils.copyProperties(param, CustomerAddrFrom.class), new EntityWrapper<CustomerAddrFrom>().where("from_addr_id={0}", param.getFromAddrId()).and("cust_id={0}", user.getCustId()));
			json.setData(param.getFromAddrId());
		}
		setJsonMessage(json, title, code, pass);
		return json;
	}

	@RequestMapping(value = "/manage/{id}", method = RequestMethod.DELETE)
	JsonObject delete(@PathVariable("id") Integer fromAddrId) throws Exception {
		JsonObject json = JsonObject.getInstance();
		CommonUserDTO user = getCommonUser();
		String title = "", code = "";
		boolean pass = true;
		
		CustomerAddrFrom data  = addrFromService.selectById(fromAddrId);
		if (Objects.isNull(data) || !Objects.equals(user.getCustId(), data.getCustId())) {
			pass = false;
			title = ErrorCodeEnum.bad_request_messsage;
			code = ErrorCodeEnum.bad_request_code;
		}
		if (pass) {
			json.setData(addrFromService.deleteById(fromAddrId));
		}
		setJsonMessage(json, title, code, pass);
		return json;
	}
	
	@RequestMapping(value = "/manage")
	JsonObject getItemList(CustomerAddrFromDTO param) throws Exception{
		JsonObject json = JsonObject.getInstance();
		CommonUserDTO user = getCommonUser();
		addrFromService.selectList(new EntityWrapper<CustomerAddrFrom>().where("cust_id={0}", user.getCustId()));
		return json;
	}
	
	@RequestMapping(value="/manage/{id}", method = RequestMethod.GET)
	JsonObject getDetail(@PathVariable("id") Integer reachAddrId) throws Exception {
		JsonObject json = JsonObject.getInstance();
		CommonUserDTO user = getCommonUser();
		CustomerAddrFrom data = addrFromService.selectById(reachAddrId);
		if (Objects.equals(user.getCustId(), data.getCustId())) {
			json.setData(data);
		}
		setJsonMessage(json);
		return json;
		
	}
	private void initData(CustomerAddrFromDTO param) throws Exception {
		CommonUserDTO user = getCommonUser();
		param.setCustId(user.getCustId());
	}
	
	private Predicate<CustomerAddrFromDTO> validate() {
	 return s -> StringUtil.isEmpty(s.getEnglishName()) || StringUtil.isEmpty(s.getAddress())
			 || !StringUtil.isEmail(s.getEmail()) || StringUtil.isEmpty(s.getCity()) 
			 || StringUtil.isEmpty(s.getMobile()) || StringUtil.isEmpty(s.getPostcode());
	}
}
